package com.wlan.dao.manage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.wlan.dao.common.AbstractTestCase;
import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.core.util.MD5Util;
import com.wlanauth.domain.manage.Operator;
import com.wlanauth.domain.manage.User;
import com.wlanauth.service.manage.UserService;
import com.wlanauth.vo.UserVO;

public class UserTestCase extends AbstractTestCase {
	
	
	public void testQueryView(){
		
		User entity = new User();
		entity.setStatus("NONEOCT");
		
		GeneralCriteria criteria =  new GeneralCriteria(UserVO.class);
		criteria.setDataOnly(false);
		criteria.addOrder(Order.desc("activeTime"));
		if(entity!=null){
			if(entity.getUsername()!=null){
				criteria.add(Restrictions.eq("username", entity.getUsername()));
			}
			if(entity.getPassword()!=null){
				criteria.add(Restrictions.eq("bindIP", entity.getBindIP()));
			}
			if(entity.getStatus()!=null){
				if(entity.getStatus().equals("NORMAL")){ //正常
					criteria.add(Restrictions.eq("status", "NORMAL"));
				} else if(entity.getStatus().equals("EXPIRED")){ //过期
					criteria.add(Restrictions.gt("expiredTime", new Date()));
				} else if(entity.getStatus().equals("NONEOCT")){ //无流量
					criteria.add(Restrictions.sqlRestriction("maxOctets*1024*1024>sumInputOctets+sumOutputOctets"));
				}
			}
		}
		GeneralCriteriaResult<UserVO> result = criteria.executeQuery();
		
		System.out.println("结果数:"+result.getTotal());
		for(UserVO vo:result.getData()){
			System.out.println(vo.getId()+",username:"+vo.getUsername()+",sumoct:"+vo.getSumInputOctets()+vo.getSumOutputOctets());
		}
		
	}
	
	
	/**
	 * 测试查询
	 */
	public void testQuery(){
		
		//radius.RadRatioDao
		System.out.println("测试用户查询..");
//		
//		UserDaoImpl userDao = (UserDaoImpl)this.getCtx().getBean("manage.UserDao");
//		
//		
//		GeneralCriteria criteria = new GeneralCriteria(RadiusUserVO.class);
//		
//		criteria.setLimit(10);
//		criteria.setStart(0);
//		criteria.setDataOnly(false);
//		
//		User u = new User();
//		u.setUsername("zz");
//		u.setExpiredTime(new Date());
//		GeneralCriteriaResult<RadiusUserVO> result = userDao.query(criteria, null,true);
//		System.out.println(result.getTotal()+":size:"+result.getData().size());
//		for(RadiusUserVO obj:result.getData()){
//			System.out.println("id:"+obj.getId()+",usrname:"+obj.getUsername()+"总流量:"+obj.getMaxOctets()+",额定费用:"+obj.getMaxFee()+",总费用:"+obj.getSumFee());
//		}
		
		
		
		
	}
	
	/**
	 * 测试查询
	 */
	public void testRemoveToHis(){
		
		//radius.RadRatioDao
		System.out.println("测试迁移用户信息");
		
		Operator oper = new Operator();
		oper.setId("operatorTest");
		List<String> ids = new ArrayList<String>();
		ids.add("2c94a64c375da3cd01375db739370001");
		UserService userService = (UserService)this.getCtx().getBean("manage.UserService");
		userService.transUserToHis(ids, oper);
	}

//	public void testInsert(){
//		//UserDao userDao = (UserDao)this.getCtx().getBean("manage.UserDao");
//		
//		
//		UserDao userDao = (UserDao)this.getCtx().getBean("manage.UserDao");
//		
//		User user = new User();
//		
//		user.setActiveTime(new Date());
//		user.setAddress("address");
//		user.setBindIP("10.12.12");
//		user.setBirth(new Date());
//		user.setContactPhone("110");
//		user.setDesc("desc");
//		user.setDisplayname("zz");
//		user.setEmail("charles.zz.life@gmail.com");
//		user.setExpiredTime(new Date());
//		
//		
//		user.setSexId("F");
//		user.setUsername("zz");
//		user.setPassword("zzpwd");
//		user.setJoinTime(new Date());
//		user.setMaxOctets(100L);
//		userDao.save(user);
//	}
	
	public void testUpdatePass2Md5(){
		GeneralCriteria criteria = new GeneralCriteria(User.class);
		GeneralCriteriaResult<User> result = criteria.executeQuery();
		for(User u:result.getData()){
			u.setPassword(MD5Util.encode(u.getPuk()));
			
			criteria.getCriteriaQuery().update(u);
			System.out.println("更新MD5:"+u.getPassword());
		}
	}
}
